package cn.wolfcode.crm.exception;

import cn.wolfcode.crm.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
对控制器进行增强处理
 */
@ControllerAdvice
public class CRMExceptionHandler {
    /*
    捕获运行过程中出现的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public String handleException(Exception ex, Model model, HandlerMethod handlerMethod, HttpServletResponse response) throws ServletException, IOException {
        //把异常信息打印,方便我们开发的时候找bug
        ex.printStackTrace();
        //判断当前执行的方法是都是ajax的方法,通过注解ResponseBody判断
        if(handlerMethod.hasMethodAnnotation(ResponseBody.class)){
            JsonResult json = new JsonResult(false, "操作失败,请联系管理员");
            response.setContentType("application/json;charset=utf-8");
            //使用response对象输出数据(json字符串)
            response.getWriter().print(JSON.toJSONString(json));
            return null;
        }
        //如果是,则返回jsonresult的请求
        //如果不是,则返回错误视图
        model.addAttribute("ex", ex);
        return "common/error";//返回的视图
    }

    @ExceptionHandler(UnauthorizedException.class)
    public String handleUnauthorizeException(Exception ex, Model model, HandlerMethod handlerMethod, HttpServletResponse response) throws ServletException, IOException {
        //把异常信息打印,方便我们开发的时候找bug
        ex.printStackTrace();
        //判断当前执行的方法是都是ajax的方法,通过注解ResponseBody判断
        if(handlerMethod.hasMethodAnnotation(ResponseBody.class)){
            JsonResult json = new JsonResult(false, "你没有改权限,请联系管理员");
            response.setContentType("application/json;charset=utf-8");
            //使用response对象输出数据(json字符串)
            response.getWriter().print(JSON.toJSONString(json));
            return null;
        }
        //处理没有权限的异常
        //跳转到nopermission权限
        model.addAttribute("ex", ex);
        return "common/nopermission";//返回的视图
    }
}
